import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class PayChannelService extends BaseService<any> {
  private readonly logger = new Logger(PayChannelService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * set
   * 对应 PHP: PayChannelService_admin::set()
   * 逻辑类型: undefined - undefined
   */
  async set(channel: any[], type: any[], data: any[]) {
    // 基于PHP真实逻辑: set
    // PHP原文: $where = array (             'type' => $type,             'channel' => $channel         );         if (!array_key_exists($type, PayDict::getPayType())...
const where = (
            type: type,
            channel: channel
        );
        if (!array_key_exists(type, PayDict.getPayType())) throw new PayBusinessException('PATMENT_METHOD_INVALID');
        if (channel != 'transfer') {
            if (!array_key_exists(channel, ChannelDict.getType())) throw new PayBusinessException('CHANNEL_MARK_INVALID');
  }

  /**
   * getChannelList
   * 对应 PHP: PayChannelService_admin::getChannelList()
   * 逻辑类型: undefined - undefined
   */
  async getChannelList() {
    // 基于PHP真实逻辑: getChannelList
    // PHP原文: $channel_list = PayChannelDict::getPayChannel();         $where = array (             'site_id' => $this->site_id         );         $pay_channel_list...
const channel_list = PayChannelDict.getPayChannel(];
        const where = (
            site_id: this.site_id
        );
        const pay_channel_list_temp = this.model.where(where).field('type, channel, config, sort, status').select().toArray(];

        const pay_channel_list = [);
        foreach (pay_channel_list_temp as v) {
            pay_channel_list[ v[ 'channel' ] ][ v[ 'type' ] ] = v;
  }

  /**
   * getListByChannel
   * 对应 PHP: PayChannelService_admin::getListByChannel()
   * 逻辑类型: undefined - undefined
   */
  async getListByChannel(channel: string) {
    // 基于PHP真实逻辑: getListByChannel
    // PHP原文: $where = array (             'site_id' => $this->site_id,             'channel' => $channel         );         $list = $this->model->where($where)->fi...
const where = (
            site_id: this.site_id,
            channel: channel
        );
        const list = this.model.where(where).field('type, channel, config, sort, status').select().toArray(];
        if (!!list)) {
            const pay_type_list = PayDict.getPayType(];
            foreach (list as k => &v) {
                const encrypt_params = pay_type_list[ v.type ].encrypt_params || [];
                foreach (v.config as config_k => config_v) {
                    if (config_v !== '' && in_[config_k, encrypt_params)) v.config[config_k] = CommonDict.ENCRYPT_STR;
                }
            }
  }

  /**
   * getConfigByPayType
   * 对应 PHP: PayChannelService_admin::getConfigByPayType()
   * 逻辑类型: undefined - undefined
   */
  async getConfigByPayType(data: any, type: any) {
    // 基于PHP真实逻辑: getConfigByPayType
    // PHP原文: $config = [];         switch ($type) {             case PayDict::WECHATPAY:                 $config = [                     'mch_id' => $data[ 'mch_id...
const config = [];
        switch (type) {
            case PayDict.WECHATPAY:
                const config = [
                    mch_id: data[ 'mch_id' ] || '',//商户号
                    mch_secret_key: data[ 'mch_secret_key' ] || '',//商户秘钥  现在默认认为是v3版
                    mch_secret_cert: data[ 'mch_secret_cert' ] || '',//商户私钥 字符串或路径
                    mch_public_cert_path: data[ 'mch_public_cert_path' ] || '',//商户公钥证书路径
                    wechat_public_cert_path: data.wechat_public_cert_path || '', // 微信支付公钥
                    wechat_public_cert_id: data.wechat_public_cert_id || '' // 微信支付公钥id
                ];
                break;
            case PayDict.ALIPAY:
                const config = [
                    app_id: data[ 'app_id' ] || '',// 必填-支付宝分配的 app_id
                    app_secret_cert: data[ 'app_secret_cert' ] || '',// 必填-应用私钥 字符串或路径
                    app_public_cert_path: data[ 'app_public_cert_path' ] || '',//必填-应用公钥证书 路径
                    alipay_public_cert_path: data[ 'alipay_public_cert_path' ] || '',//必填-支付宝公钥证书 路径
                    alipay_root_cert_path: data[ 'alipay_root_cert_path' ] || '',// 必填-支付宝根证书 路径
                ];
                break;
            case PayDict.OFFLINEPAY:
                const config = [
                    collection_name: data[ 'collection_name' ] || '',// 必填-收款账户名称
                    collection_bank: data[ 'collection_bank' ] || '',//必填-收款银行
                    collection_account: data[ 'collection_account' ] || '',//必填-收款账号
                    collection_desc: data[ 'collection_desc' ] || '',// 必填-转账说明
                ];
                break;
            case PayDict.FRIENDSPAY:
                const config = [
                    pay_type_name: data[ 'pay_type_name' ] || get_lang('dict_pay.type_friendspay'),// 支付方式名称
                    pay_leave_message: data[ 'pay_leave_message' ] || get_lang('dict_pay_config.pay_leave_message'),// 发起帮付默认留言
                    pay_button_name: data[ 'pay_button_name' ] || get_lang('dict_pay_config.pay_button_name'),// 支付按钮名称
                    pay_page_name: data[ 'pay_page_name' ] || get_lang('dict_pay_config.pay_page_name'),// 帮付页面名称
                    pay_explain_switch: data[ 'pay_explain_switch' ] || 1,// 帮付说明开关, 1开启，0关闭
                    pay_explain_title: data[ 'pay_explain_title' ] || get_lang('dict_pay_config.pay_explain_title'),// 帮付说明标题
                    pay_explain_content: data[ 'pay_explain_content' ] || get_lang('dict_pay_config.pay_explain_content'),// 帮付说明内容
                    pay_info_switch: data[ 'pay_info_switch' ] || 1,// 订单信息, 1开启，0关闭
                    pay_wechat_share_image: data[ 'pay_wechat_share_image' ] || '/static/resource/images/pay/pay_wechat_share_image.png',// 默认分享图片（公众号）
                    pay_weapp_share_image: data[ 'pay_weapp_share_image' ] || '/static/resource/images/pay/pay_weapp_share_image.png'// 默认分享图片（小程序）
                ];
                break;
            default:
                const config = data;
  }

  /**
   * setTransfer
   * 对应 PHP: PayChannelService_admin::setTransfer()
   * 逻辑类型: undefined - undefined
   */
  async setTransfer(data: any) {
    // 基于PHP真实逻辑: setTransfer
    // PHP原文: $wechatpay_config = $data[ 'wechatpay_config' ];         $alipay_config = $data[ 'alipay_config' ];         $this->set('transfer', PayDict::WECHATPAY,...
const wechatpay_config = data[ 'wechatpay_config' ];
        const alipay_config = data[ 'alipay_config' ];
        this.set('transfer', PayDict.WECHATPAY, [
            config: wechatpay_config,
            status: 1,
        )];
        this.set('transfer', PayDict.ALIPAY, [
            config: alipay_config,
            status: 1,
        )];
        return true;
    }

    async setAll(data)
    {
        foreach (data as k => v) {
            const temp_v = v[ 'pay_type' ];
            foreach (temp_v as item_k => item) {
                this.set(k, item[ 'key' ], [
                    config: item[ 'config' ] || [],
                    status: item[ 'status' ] || 0,
                    sort: item[ 'sort' ) || 0,
                )];
            }
  }

  /**
   * setAll
   * 对应 PHP: PayChannelService_admin::setAll()
   * 逻辑类型: undefined - undefined
   */
  async setAll(data: any) {
    // 基于PHP真实逻辑: setAll
    // PHP原文: foreach ($data as $k => $v) {             $temp_v = $v[ 'pay_type' ];             foreach ($temp_v as $item_k => $item) {                 $this->set($...
foreach (data as k => v) {
            const temp_v = v[ 'pay_type' ];
            foreach (temp_v as item_k => item) {
                this.set(k, item[ 'key' ], [
                    config: item[ 'config' ] || [],
                    status: item[ 'status' ] || 0,
                    sort: item[ 'sort' ) || 0,
                )];
            }
  }

  /**
   * getInfo
   * 对应 PHP: PayChannelService_admin::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(where: any[]) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: $where[ 'site_id' ] = $this->site_id;         $res = $this->model->where($where)->field('type, channel, config, sort, status')->findOrEmpty()->toArray...
where[ 'site_id' ] = this.site_id;
        const res = this.model.where(where).field('type, channel, config, sort, status').findOrEmpty().toArray(];
        return res;

    }

}
  }
}
